package Java7_Concurrency_Cookbook.thread_synchronized_helper.exchanger;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Exchanger;

/**
 * Exchanger 类允许两个线程之间定义同步点 (Synchronization Point)<br/>
 * 当两个线程都到达同步点时, 它们交换数据结构<br/>
 * 因此第一个线程的数据结构进入到第二个线程, 同时第二个线程的数据结构进入到第一个线程
 * @author lixiangke
 * @date 2017/11/16
 */
public class Core {
    public static void main(String[] args) {
        List<String> buffer1 = new ArrayList<>();
        List<String> buffer2 = new ArrayList<>();
        Exchanger<List<String>> exchanger = new Exchanger<>();
        Producer producer = new Producer(buffer1, exchanger);
        Consumer consumer = new Consumer(buffer2, exchanger);
        Thread threadProducer = new Thread(producer, "Producer");
        Thread threadConsumer = new Thread(consumer, "Consumer");
        threadProducer.start();
        threadConsumer.start();
    }
}
